package cn.itsource.crm.web.controller;

import cn.itsource.crm.domain.Employee;
import cn.itsource.crm.query.EmployeeQuery;
import cn.itsource.crm.service.IEmployeeService;
import cn.itsource.util.AjaxResult;
import cn.itsource.util.PageList;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/employee")
@CrossOrigin//解决跨域问题
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;



    /**
     * 分页和高级查询
     * @return
     */
    @RequestMapping(value = "/queryPage",method = RequestMethod.PATCH)//批量查询用PATCH
    @ResponseBody
    @ApiOperation(value = "list查询所有方法",notes = "查询所有员工信息")
    //HTTP响应整体描述
    @ApiResponses({
            //HTTP响应其中1个描述
            @ApiResponse(code = 200,message = "成功"),
            @ApiResponse(code = 500,message = "网络错误，请稍后")
    })
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery employeeQuery){
        PageList<Employee> list = employeeService.queryPage(employeeQuery);
        return list;
    }

    /**
     * 查询所有数据信息
     * @return
     */
    @RequestMapping(value = "/list/{tenantId}",method = RequestMethod.GET)//批量查询用PATCH
    @ResponseBody
    @ApiOperation(value = "list查询所有方法",notes = "查询所有员工信息")
    //HTTP响应整体描述
    @ApiResponses({
            //HTTP响应其中1个描述
            @ApiResponse(code = 200,message = "成功"),
            @ApiResponse(code = 500,message = "网络错误，请稍后")
    })
    public List<Employee> list(@PathVariable Long tenantId){/*tenantId要跟上面的{tenantId}*/
        List<Employee> all = employeeService.findAllByTenant(tenantId);
        return all;
    }

    /**
     * 查询所有数据信息
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.PATCH)//批量查询用PATCH
    @ResponseBody
    @ApiOperation(value = "list查询所有方法",notes = "查询所有员工信息")
    //HTTP响应整体描述
    @ApiResponses({
            //HTTP响应其中1个描述
            @ApiResponse(code = 200,message = "成功"),
            @ApiResponse(code = 500,message = "网络错误，请稍后")
    })
    public List<Employee> list(){
        List<Employee> all = employeeService.findAll();
        return all;
    }


    /**
     * 根据id查询单个员工信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/findOne/{id}",method = RequestMethod.GET)//查询单个用GET
    @ResponseBody
    //描述一个类的一个方法，或者说一个接口
    @ApiOperation(value = "根据id查询员工信息",notes = "getOne查询方法")
    //一个请求参数
    @ApiImplicitParam(name = "id",value = "id参数",required = true,dataType = "Long",paramType = "path")
    public Employee findOne(@PathVariable Long id){
        System.out.println("通过id获取单个对象--  " + id);
        return employeeService.findOne(id);
    }

    /**
     * 保存员工信息
     * @param employee
     * @return
     */
    @RequestMapping(value = "/save",method = RequestMethod.PUT)//保存用PUT
    @ResponseBody
    @ApiOperation(value = "save保存员工信息的方法",notes = "保存员工信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "employee",value = "employee对象",dataType = "Employee",paramType = "path")
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "保存成功"),
            @ApiResponse(code = 500,message = "服务器错误")
    })
    public AjaxResult save(@RequestBody Employee employee){
        try {
            /*默认账号状态为true*/
            employee.setState(true);
            employeeService.save(employee);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            AjaxResult result = new AjaxResult(false);
            result.setMsg("操作失败"+e.getMessage());
            return result;
        }
    }

    /**
     * 更新员工信息
     * @param employee
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)//POST用来修改信息
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200,message = "更新成功"),
            @ApiResponse(code = 500,message = "网络错误...")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "employee员工信息",value = "员工信息",dataType = "Employee",paramType = "path")
    })
    public AjaxResult update(@RequestBody Employee employee){
        try {
            employeeService.update(employee);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false);
        }
    }

    /**
     * 员工绑定角色
     * @param employee
     * @return
     */
    @RequestMapping(value = "/employeeBindRole",method = RequestMethod.POST)//POST用来修改信息
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200,message = "更新成功"),
            @ApiResponse(code = 500,message = "网络错误...")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "员工绑定角色",value = "员工绑定角色",dataType = "Employee",paramType = "path")
    })
    public AjaxResult employeeBindRole(@RequestBody Employee employee){
        try {
            employeeService.employeeBindRole(employee);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false);
        }
    }


    @RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)//DELETE删除员工信息
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200,message = "删除成功"),
            @ApiResponse(code = 500,message = "服务器异常")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "参数id",value = "参数id",dataType = "Long",paramType = "path",required = true)
    })
    @ApiOperation(value = "delete删除方法",notes = "删除员工信息")
    public AjaxResult delete(@PathVariable Long id){
        try {
            employeeService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false);
        }
    }
}
